package com.im;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.im.common.config.MinioConfig;
import com.im.common.util.GenerateIdUtil;
import com.im.project.entity.Book;
import com.im.project.mapper.BookMapper;
import com.im.project.webSocket.UserChannelCtxMap;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 测试
 *
 * @author yunZhouJiaDi
 */
@Slf4j
@SpringBootTest
public class test {

    @Resource
    BookMapper bookMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    MinioClient minioClient;

    @Resource
    MinioConfig minioConfig;

    @Test
    void addBook() {
        bookMapper.insert(new Book().setName("java"));
    }

    @Test
    void getBookById() {
        Book book = bookMapper.selectById(34);
        log.info("book的值：{}", book);
    }

    @Test
    void updateBook() {
        bookMapper.update(new Book().setName("Golang"), new QueryWrapper<Book>().eq("id", "34"));
    }

    @Test
    void queryBookList() {
        List<Book> bookList = bookMapper.selectList(new QueryWrapper<Book>());
        log.info("book列表：{}", bookList);
    }

    @Test
    void saveRedis() {
        redisTemplate.opsForValue().set("redis", "中间件", 5L, TimeUnit.MINUTES);
    }

    @Test
    void getRedis() {
        Object o = redisTemplate.opsForValue().get("redis");
        log.info("获取redis缓存：{}", o);
    }

    @Test
    void genUserId() {
        String userId = GenerateIdUtil.generateUserId();
        log.info("生成的用户id:{}", userId);
    }

    @Test
    void minioUpload() {
        String username = "云洲泇滴";
        Color usernameColor = Color.CYAN;
        Color backgroundColor = Color.LIGHT_GRAY;
        BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(backgroundColor);
        g2d.fillRect(0, 0, 200, 200);
        Font font = new Font("宋体", Font.BOLD, 100);
        g2d.setFont(font);
        g2d.setColor(usernameColor);
        String initials = username.substring(0, 1);
        FontMetrics fontMetrics = g2d.getFontMetrics();
        Rectangle2D rect = fontMetrics.getStringBounds(initials, g2d);
        int centerX = (200 - (int) rect.getWidth()) / 2;
        int centerY = (200 - (int) rect.getHeight()) / 2 + fontMetrics.getAscent();
        g2d.drawString(initials, centerX, centerY);
        ByteArrayOutputStream baas = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", baas);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] imageBytes = baas.toByteArray();
        String objectName = UUID.randomUUID().toString() + ".png";
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minioConfig.getBucketName())
                    .object(objectName)
                    .stream(new ByteArrayInputStream(imageBytes), imageBytes.length, -1)
                    .contentType("image/png")
                    .build());
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }
        log.info("地址为：{}", objectName);
    }

    @Test
    void generateUserChatId() {
        String chatId = GenerateIdUtil.generateUserChatId(new String[]{"U091953631", "U083552857"});
        log.info("生成会话id:{}", chatId);
    }

    @Test
    void generateMessageId() {
        String messageId = GenerateIdUtil.generateMessageId();
        log.info("生成消息id:{}", messageId);
    }

    @Test
    void generateClusterId() {
        String clusterId = GenerateIdUtil.generateClusterId();
        log.info("生成群组id:{}", clusterId);
    }

    @Test
    void generateClusterChatId() {
        String chatId = GenerateIdUtil.generateClusterChatId("G092502574");
        log.info("生成群聊会话id:{}", chatId);
    }

}
